package com.yellowpage.omc.ability.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.nsp.dao.RemoteNspDAO;
import com.yellowpage.omc.ability.data.AppInterface;
import com.yellowpage.omc.ability.data.AppInterfaceParam;
import com.yellowpage.omc.ability.data.AppInterfaceParamValue;
import com.yellowpage.omc.ability.data.Partner;
import com.yellowpage.omc.ability.data.PartnerInterface;
import com.yellowpage.omc.ability.data.PartnerParamValue;
import com.yellowpage.omc.ability.service.AppInterfaceService;
import com.yellowpage.omc.ability.service.PartnerInterfaceService;
import com.yellowpage.omc.ability.service.PartnerService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class PartnerInterfaceAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	public String RESULT_PARTNER = "Synchronous";
	
	
	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<PartnerInterface> partnerInterfaceList = new ArrayList<PartnerInterface>();
	private List<Map> resultList = new ArrayList<Map>();

	private PartnerInterface partnerInterface;
	private PartnerInterfaceService partnerInterfaceService;
	private AppInterfaceService appInterfaceService;
	private PartnerService partnerService;
	
	//-----------------------------
	private AppInterface appInterface;
	private AppInterfaceParam appInterfaceParam;
	private AppInterfaceParamValue appInterfaceParamValue;
	
	private Partner partner;
	private PartnerParamValue partnerParamValue;

	
	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_partnerInterfaceId;
	private String query_partnerId;
	private String query_interfaceId;
	private String query_createUser;
	private String query_createTime;
	private String query_lastUpdateUser;
	private String query_lastUpdateTime;
	private String query_status;
	
	private String query_LastSynchronizationUser;
	private String query_LastSynchronizationTime;

	private Map partnerMap = new HashMap();
	private Map appInterfaceMap = new HashMap();

	public void prepare() {
		super.prepare();
		
		partnerMap = partnerService.getPartnerMap();
		appInterfaceMap = appInterfaceService.getAppInterfaceMap();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.partnerInterface = partnerInterfaceService
					.getPartnerInterface(Long.parseLong(selectedId));
		}
	}

	public PartnerService getPartnerService() {
		return partnerService;
	}

	public void setPartnerService(PartnerService partnerService) {
		this.partnerService = partnerService;
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("partnerInterfaceId", query_partnerInterfaceId);
		queryMap.put("partnerId", query_partnerId);
		queryMap.put("interfaceId", query_interfaceId);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createTime", query_createTime);
		queryMap.put("lastUpdateUser", query_lastUpdateUser);
		queryMap.put("lastUpdateTime", query_lastUpdateTime);
		queryMap.put("status", query_status);
		queryMap.put("LastSynchronizationUser", query_LastSynchronizationUser);
		queryMap.put("LastSynchronizationTime", query_LastSynchronizationTime);

		resultList = partnerInterfaceService.queryPartnerInterfaceByPage(
				queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	//
	public String partnerNsptoUpdate() {
		try {
			Long userId = loginUser.getUserId();
			partnerInterfaceService.queryAbilityPartner(userId);
			partnerInterfaceService.queryAbilityPartnerInterface(userId);
			partnerInterfaceService.queryAbilityPartnerParamValue(userId);
			partnerInterfaceService.queryAbilityInterfaceParam(userId);
			partnerInterfaceService.queryAbilityInterface(userId);
			partnerInterfaceService.queryAbilityInterfaceParamValue(userId);
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		message = "同步数据成功！";
		return RESULT_PARTNER;
		
	}
	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		
		partnerInterface.setCreateTime(new Date());
		partnerInterface.setCreateUser(loginUser.getUserId());
		partnerInterface.setLastUpdateTime(new Date());
		partnerInterface.setLastUpdateUser(loginUser.getUserId());
		partnerInterface.setStatus("1");

		partnerInterfaceService.savePartnerInterface(partnerInterface);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		

		this.setValidatorResult("/omc/ability/PartnerInterfaceAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		partnerInterface = partnerInterfaceService.getPartnerInterface(id);

		return RESULT_EDIT;
	}

	public String update() {
		
		partnerInterface.setLastUpdateTime(new Date());
		partnerInterface.setLastUpdateUser(loginUser.getUserId());

		partnerInterfaceService.updatePartnerInterface(partnerInterface);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {

		this.setValidatorResult("/omc/ability/PartnerInterfaceUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.partnerInterfaceService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突，删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setPartnerInterface(PartnerInterface partnerInterface) {
		this.partnerInterface = partnerInterface;
	}

	public PartnerInterface getPartnerInterface() {
		return partnerInterface;
	}

	public void setPartnerInterfaceService(
			PartnerInterfaceService partnerInterfaceService) {
		this.partnerInterfaceService = partnerInterfaceService;
	}

	public List<PartnerInterface> getPartnerInterfaceList() {
		return partnerInterfaceList;
	}

	public void setPartnerInterfaceList(
			List<PartnerInterface> partnerInterfaceList) {
		this.partnerInterfaceList = partnerInterfaceList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_partnerInterfaceId() {
		return this.query_partnerInterfaceId;
	}

	public void setQuery_partnerInterfaceId(String partnerInterfaceId) {
		this.query_partnerInterfaceId = partnerInterfaceId;
	}

	public String getQuery_partnerId() {
		return this.query_partnerId;
	}

	public void setQuery_partnerId(String partnerId) {
		this.query_partnerId = partnerId;
	}

	public String getQuery_interfaceId() {
		return this.query_interfaceId;
	}

	public void setQuery_interfaceId(String interfaceId) {
		this.query_interfaceId = interfaceId;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_lastUpdateUser() {
		return this.query_lastUpdateUser;
	}

	public void setQuery_lastUpdateUser(String lastUpdateUser) {
		this.query_lastUpdateUser = lastUpdateUser;
	}

	public String getQuery_lastUpdateTime() {
		return this.query_lastUpdateTime;
	}

	public void setQuery_lastUpdateTime(String lastUpdateTime) {
		this.query_lastUpdateTime = lastUpdateTime;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public AppInterfaceService getAppInterfaceService() {
		return appInterfaceService;
	}

	public void setAppInterfaceService(AppInterfaceService appInterfaceService) {
		this.appInterfaceService = appInterfaceService;
	}

	public PartnerInterfaceService getPartnerInterfaceService() {
		return partnerInterfaceService;
	}

	public Map getPartnerMap() {
		return partnerMap;
	}

	public void setPartnerMap(Map partnerMap) {
		this.partnerMap = partnerMap;
	}

	public Map getAppInterfaceMap() {
		return appInterfaceMap;
	}

	public void setAppInterfaceMap(Map appInterfaceMap) {
		this.appInterfaceMap = appInterfaceMap;
	}
	
	/**
	 * @return the query_LastSynchronizationUser
	 */
	public String getQuery_LastSynchronizationUser() {
		return query_LastSynchronizationUser;
	}

	/**
	 * @param query_LastSynchronizationUser the query_LastSynchronizationUser to set
	 */
	public void setQuery_LastSynchronizationUser(
			String query_LastSynchronizationUser) {
		this.query_LastSynchronizationUser = query_LastSynchronizationUser;
	}

	/**
	 * @return the query_LastSynchronizationTime
	 */
	public String getQuery_LastSynchronizationTime() {
		return query_LastSynchronizationTime;
	}

	/**
	 * @param query_LastSynchronizationTime the query_LastSynchronizationTime to set
	 */
	public void setQuery_LastSynchronizationTime(
			String query_LastSynchronizationTime) {
		this.query_LastSynchronizationTime = query_LastSynchronizationTime;
	}


}
